/**
 * 
 */
package com.matchmealpha;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * @author Panda
 * 
 */
public class SettingActivity extends Activity implements OnClickListener {

	private Button leftButton;
	private Button rightButton;
	private int callerID;
	protected Button changePasswordButton;
	protected Button changePicButton;
	protected Button connectToButton;
	protected Button findFriendsButton;
	protected Button logoutButton;
	protected String sessionid;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initLayout();
		initListener();
		SharedPreferences settings = getSharedPreferences(
				"matchme_preferences", 0);
		String sessionid = settings.getString("sessionid", "");
	}

	private void initLayout() {
		setContentView(R.layout.setting_view);
		leftButton = (Button) this.findViewById(R.id.SettingLeftButton);
		rightButton = (Button) this.findViewById(R.id.SettingRightButton);
		changePasswordButton = (Button) this
				.findViewById(R.id.SettingChangePasswordButton);
		changePicButton = (Button) this
				.findViewById(R.id.SettingChangePicButton);
		connectToButton = (Button) this
				.findViewById(R.id.SettingConnectToButton);
		findFriendsButton = (Button) this
				.findViewById(R.id.SettingFindFriendsButton);
		logoutButton = (Button) this
		.findViewById(R.id.SettingLogoutButton);
	}

	private void initListener() {
		leftButton.setOnClickListener(this);
		rightButton.setOnClickListener(this);
		changePasswordButton.setOnClickListener(this);
		changePicButton.setOnClickListener(this);
		connectToButton.setOnClickListener(this);
		findFriendsButton.setOnClickListener(this);
		logoutButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case (R.id.SettingLeftButton):
			TimelineGroup.timelineGroup.back();
			break;
		case (R.id.SettingRightButton):
			//TODO Save picture
			TimelineGroup.timelineGroup.back();
			break;
		case (R.id.SettingChangePasswordButton):
			Intent intent = new Intent(this, ChangePasswordActivity.class);
			View view = TimelineGroup.timelineGroup.getLocalActivityManager()
					.startActivity("ChangePasswordActivity",
							intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
					.getDecorView();
			TimelineGroup.timelineGroup.replaceView(view);
			break;
		case (R.id.SettingChangePicButton):

			break;
		case (R.id.SettingConnectToButton):
			Intent intent2 = new Intent(this, ConnectToActivity.class);
			View view2 = TimelineGroup.timelineGroup.getLocalActivityManager()
					.startActivity("ConnectToActivity",
							intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
					.getDecorView();
			TimelineGroup.timelineGroup.replaceView(view2);
			break;
		case (R.id.SettingFindFriendsButton):
			Intent intent3 = new Intent(this, FindFriendsActivity.class);
			View view3 = TimelineGroup.timelineGroup.getLocalActivityManager()
					.startActivity("FindFriendsActivity",
							intent3.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
					.getDecorView();
			TimelineGroup.timelineGroup.replaceView(view3);
			break;
			
		case (R.id.SettingLogoutButton):
			

			SharedPreferences settings = getSharedPreferences(
					"matchme_preferences", 0);
			SharedPreferences.Editor editor = settings.edit();
			editor.clear();
			editor.commit();
			new LogoutTask().execute("");
			MatchMeTabWidget.THIS_INSTANCE.finish();
			break;
		}
	}
	
	private class LogoutTask extends AsyncTask<String, Void, Boolean> {

		protected Boolean doInBackground(String... data) {

			HttpRequest req = new HttpRequest(sessionid);
			req.sendPost(MatchMeTabWidget.MM_SERVER + "/logout_user", "");
			return true;
					
		}
		protected void onPostExecute(Boolean result) {
			
		}
	}

}
